package top.vanchor.myblog2back.controller.extra;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import top.vanchor.myblog2back.entity.ExtraBloodSugar;
import top.vanchor.myblog2back.service.IExtraBloodSugarService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-04-06
 */
@CrossOrigin
@RestController
@RequestMapping("/extra/bloodSugar")
public class ExtraBloodSugarController {

    @Resource
    private IExtraBloodSugarService service;

    /**
     * 获取血糖数据
     * @return json
     */
    @RequestMapping("/getList")
    public JSONObject get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        JSONObject jsonObject = new JSONObject();
        PageInfo<ExtraBloodSugar> list = service.getList(pageNo,pageSize);
        jsonObject.put("code", 200);
        jsonObject.put("msg", "success");
        jsonObject.put("data", list);
        return jsonObject;
    }

    /**
     * 添加血糖数据
     * @return json
     */
    @RequestMapping("/add")
    public JSONObject add() {
        JSONObject jsonObject = new JSONObject();
        ExtraBloodSugar bloodSugar = new ExtraBloodSugar();
        bloodSugar.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        System.out.println(bloodSugar);
        int result = service.add(bloodSugar);
        if (result > 0) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "failed");
        }
        return jsonObject;
    }

    /**
     * 添加血糖数据
     * @return json
     */
    @RequestMapping("/doDelete")
    public JSONObject doDelete(@RequestBody JSONObject data) {
        JSONObject jsonObject = new JSONObject();
        Integer x = Integer.parseInt(String.valueOf(data.get("position_x")));
        ExtraBloodSugar bloodSugar = new ExtraBloodSugar();
        bloodSugar.setBloodId(x);
        boolean result = service.removeById(bloodSugar.getBloodId());
        if (result) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "success");
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "failed");
        }
        return jsonObject;
    }

    /**
     * 编辑血糖数据
     * @param data json
     * @return json
     */
    @RequestMapping("/doEdit")
    public JSONObject add(@RequestBody JSONObject data) {
        JSONObject jsonObject = new JSONObject();
        double newValue = Double.parseDouble(String.valueOf(data.get("newValue")));
        Integer x = Integer.parseInt(String.valueOf(data.get("position_x")));
        Integer y = Integer.parseInt(String.valueOf(data.get("position_y")));
        ExtraBloodSugar bloodSugar = service.getById(x);
        bloodSugar.setBloodId(x);
        switch (y) {
            case 1:
                bloodSugar.setFastingValue(newValue);break;
            case 2:
                bloodSugar.setBreakfastValue(newValue);break;
            case 3:
                bloodSugar.setLunchValue(newValue);break;
            case 4:
                bloodSugar.setDinnerValue(newValue);break;
            case 5:
                bloodSugar.setSleepValue(newValue);break;
            default:
                break;
        }
        if (service.updateItem(bloodSugar) >= 0) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "success");
        }else{
            jsonObject.put("code", 500);
            jsonObject.put("msg", "error");
        }
        return jsonObject;
    }
}
